package net.springmvc11_12.controller;

import net.springmvc11_12.entity.Stu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/stu")
public class StuController {

//    @RequestMapping(value = "/hh",method = RequestMethod.GET)
    @GetMapping("hh")
    public String hh() {
        return "hh";
    }

    @GetMapping("data")
    public String data(@RequestParam(value = "name") String name,
                       @RequestParam(required = false,defaultValue = "111") String sn) {
        System.out.println(name+" "+sn);
        return "hh";
    }

    @GetMapping("data1")
    public String data1(@RequestParam List<String> name) {
        System.out.println(name);
        return "hh";
    }

    // 实体接收
    @GetMapping("data2")
    public String data2(Stu stu) {
        System.out.println(stu);
        return "hh";
    }

    //@PathVariable注解

    @GetMapping("data3/{id1}")
    public String data3(@PathVariable("id1") Integer id) {
        System.out.println(id);
        return "hh";
    }

    //@RequestBody
    @GetMapping("data4")
    public String data4(@RequestBody Stu stu) {
        System.out.println(stu);
        return "hh";
    }

    //@CookieValue
    @GetMapping("data5")
    public String data5(@CookieValue(value = "JSESSIONID") String sessionId,
                        @CookieValue("cookie") String cookie) {
        System.out.println(sessionId+" "+cookie);
        return "hh";
    }
    //@RequestHeader注解
    @GetMapping("data6")
    public String data6(@RequestHeader("Host") String host,
                        @RequestHeader("Cookie") String cookie) {
        System.out.println(host+" "+cookie);
        return "hh";
    }

    // request 级别作用域
    @GetMapping("data7")
    public String data7(Model model) {
        model.addAttribute("mode", "modeValue");
        return "hh";
    }
    @GetMapping("data8")
    public String data8(ModelMap modelMap) {
        modelMap.addAttribute("mode", "modelMapValue");
        return "hh";
    }

    @GetMapping("data9")
    public String data9(Map<String, Object> map) {
        map.put("mode", "mapValue");
        return "hh";
    }
    @GetMapping("data10")
    public String data10(HttpServletRequest request) {
        request.setAttribute("mode", "requestValue");
        return "hh";
    }

    @GetMapping("data11")
    public ModelAndView data11(ModelAndView modelAndView) {
        modelAndView.addObject("mode", "modelValue");
        modelAndView.setViewName("hh");
        return modelAndView;
    }

    // Session级别属性（共享）域
    @GetMapping("data12")
    public String data12(HttpSession session) {
        session.setAttribute("mode", "sessionValue");
        return "hh";
    }

    // Application级别属性（共享）域
    @Autowired
    private ServletContext servletContext;
    @GetMapping("data13")
    public String data13() {
        servletContext.setAttribute("mode", "servletContextValue");
        return "hh";
    }
    // 转发 重定向
    // 重定向 redirect
    @GetMapping("redirect")
    public String data14() {
//        request.getRequestDispatcher("/xx").forward(request, response);
//        response.sendRedirect("/xx");
        return "redirect:/index.jsp";
    }
    @GetMapping("forward")
    public String data15() {
        return "forward:/stu/data12";
    }
}
